package com.imu.purse.modules.trade.service.impl;

import java.util.Date;
import java.util.List;

import com.imu.enums.DetailLogType;
import com.imu.purse.modules.bitcoin.form.YtfcoinForm;
import com.imu.purse.modules.trade.dao.TransDetailLogMapper;
import com.imu.purse.modules.trade.entity.TransDetailLogEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.imu.core.base.BaseService;
import com.imu.datasources.DataSourceNames;
import com.imu.datasources.annotation.DataSource;
import com.imu.purse.modules.trade.dao.TransLogMapper;
import com.imu.purse.modules.trade.entity.TransLogEntity;
import com.imu.purse.modules.trade.service.TransLogService;

/**
 * IMU转账日志表
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-07-01 16:21:08
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class TransLogServiceImpl extends BaseService<TransLogEntity> implements TransLogService
{
    @Override
    @Transactional
    public int save(final TransLogEntity transLog)
    {
        transLog.setCreateTime(new Date());
        return super.insert(transLog);
    }

    @Override
    @Transactional
    public int update(final TransLogEntity transLog)
    {
        transLog.setUpdateTime(new Date());
        return mapper.updateByPrimaryKeySelective(transLog);
    }

    @Override
    @Transactional
    public int saveUpdate(final TransLogEntity transLog)
    {
        int len = 0;

        if(1 > (len = this.update(transLog)))
        {
            len = this.insert(transLog);
        }

        return len;
    }

    @Override
    @Transactional
    public boolean delete(final String ...ids)
    {
        if(ArrayUtil.isEmpty(ids)) return false;

        for (String id : ids)
        {
            if(StrUtil.isBlank(id)) continue;

            if(NumberUtil.isNumber(id))
            {
                mapper.deleteByPrimaryKey(Long.valueOf(id));
            }
        }

        return true;
    }

    @Override
    @DataSource(name=DataSourceNames.SECOND)
    public TransLogEntity findById(final Long id)
    {
        if(null == id) return null;
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    @DataSource(name=DataSourceNames.SECOND)
    public List<TransLogEntity> queryList(final TransLogEntity transLog)
    {
        if (transLog.getPage() != null && transLog.getLimit() != null)
        {
            PageHelper.startPage(transLog.getPage(), transLog.getLimit());
        }

        return mapper.findByPage(transLog);
    }


    /**
     * A-->A1 A转以太坊到A1
     * @param form
     * @return
     */
    @Transactional
    public int moYtfLog(final YtfcoinForm form){
        //根据交易ID和时间获取信息
        TransLogEntity entity = new TransLogEntity();
        entity.setTransId(form.getTransId());
        entity.setTransTime(form.getTransTime());
        entity = mapper.findByTId(entity);
        if(entity != null){
            TransLogEntity tEntity = new TransLogEntity();
            tEntity.setId(entity.getId());
            tEntity.setOrderState(DetailLogType.FEE_FIN.getType());
            tEntity.setUpdateTime(new Date());
            mapper.updateByPrimaryKeySelective(tEntity);
            //修改详情表的信息
            TransDetailLogEntity logEntity = new TransDetailLogEntity();
            logEntity.setTransId(form.getTransId());
            logEntity.setMoney(form.getMoney());
            logEntity.setUpdateTime(tEntity.getUpdateTime());
            transDetailLogMapper.moByTId(logEntity);
        }
        return 1;
    }

    @Autowired
    private TransLogMapper mapper;

    @Autowired
    private TransDetailLogMapper transDetailLogMapper;
}
